﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet.Entity.Common.Web;
using BaseSet;
using System.Data;

namespace WebSite.B
{
    public partial class BlogDetail : PreBasePage
    {
        public string blogid = "";
        private Users uinfo = null;
        private Blog binfo = null;
        public string strseo = "";
        public string userlink = "";
        public string companylink = "";
        public string ownerlink = "";
        public string youbloguserid = "";
        public string blogcompanyid = "";
        public string companyowneruserid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            blogid = Request.QueryString["id"] + "";
            if (blogid.Contains(","))
            {
                string [] str=blogid.Split(',');
                int len = str.Length;
                blogid = str[len-1];
            }
            if (blogid == "")
            {
                Response.Redirect("../Index.aspx");
                return;
            }

            uinfo = UserBase.GetCurrentUser();//当前用户
            if (uinfo != null)
            {
                hidCurrentUserID.Value = uinfo.ID;
            }
            BlogHelper bhelper = new BlogHelper();
            binfo = bhelper.GetBlog(blogid);
            //判断当前博客的状态
            if (binfo == null || binfo.Flag == -9 || binfo.Flag == 9)
            {
                Response.Redirect("../NotFind.aspx");
                return;
            }

            if (!Page.IsPostBack)
            {
                LoadContent();
                LoadReply();
                BindUserBlog();
                BindCompany();//加载小组信息
                BindRecommand();
                BindOtherBlog();
                BindCompanyBlog();//加载同小组其它博客
                BindPV();

                BindSeo();

                BindFrontBlog();//作者前一篇
                BindBehindBlog();//作者后一篇

                BindCompanyList();
                BindAd();//加载广告
            }
        }

        #region 加载内容
        private void LoadContent()
        {
            if (binfo == null)
            {
                Response.Redirect("../Index.aspx");
                return;
            }
            hidID.Value = binfo.ID;
            hidBlogUserID.Value = binfo.UsersID;
            hidBlogTypeID.Value = binfo.BlogTypeID;
            youbloguserid = binfo.UsersID;
            userlink = "../u/" + binfo.UsersID + ".aspx";
            litTitle.Text = binfo.Title;
            litCreateDate.Text = binfo.CreateDate.ToString("yyyy-MM-dd");
            litNickName.Text = binfo.NickName;

            litPVCount.Text = binfo.PVCount.ToString();
            litRecommandCount.Text = binfo.RecommandCount.ToString();
            lblRecommandCount.Text = binfo.RecommandCount.ToString();
            litReplyCount.Text = binfo.ReplyCount.ToString();

            //是否允许其他人编辑
            int otheredit = binfo.OtherEdit;
            if (otheredit == 1)//可以编辑
            {
                if (uinfo == null)
                {
                    litOtherEdit.Text = "<a href=\"../Login.aspx?returnurl=b/" + blogid + ".aspx\"><font color=\"blue\">登录后允许编辑</font></a>";
                }
                else
                {
                    litOtherEdit.Text = "<a href=\"../Blogs/BlogEdit.aspx?id=" + blogid + "&otheruserid=" + uinfo.ID + "\" target=\"_blank\"><font color=\"blue\">允许编辑</font></a>";
                }
            }
            else if (otheredit == 0)//不可以编辑
            {
                litOtherEdit.Visible = false;
            }


            //评论登录按钮显示
            if (uinfo == null)
            {
                logins.Visible = true;
            }
            else
            {
                logins.Visible = false;
            }

            //加载右侧作者信息
            litNickName1.Text = binfo.NickName;
            litNickName2.Text = binfo.NickName+"推荐";
            litNickName3.Text = binfo.NickName;
            string userid = binfo.UsersID;
            Users blogUinfo = new Users();
            UsersHelper uhelper = new UsersHelper();
            blogUinfo = uhelper.GetUsers(userid);
            string roleid = blogUinfo.RoleID;
            string createdate = blogUinfo.CreateDate.ToString("yyyy-MM-dd");
            //加载权限
            Role rinfo = new Role();
            RoleHelper rhelper = new RoleHelper();
            rinfo = rhelper.GetRole(roleid);
            string rolename = rinfo.RoleName;

            string headphoto = blogUinfo.SmallHeadImage;
            if (string.IsNullOrEmpty(headphoto))
            {
                headphoto = "../images/morentouxiang.jpg";
            }
            imghead.ImageUrl = headphoto;
            imghead.ToolTip = blogUinfo.NickName;
            litRoleName.Text = rolename;
            litBrief.Text = blogUinfo.Brief;
            litBlogNum.Text = blogUinfo.BlogNum.ToString();
            litToBeFriendNum.Text = blogUinfo.ToBeFriendNum.ToString();
            litFrinedNum.Text = blogUinfo.FriendNum.ToString();
            litBlogPVNum.Text = blogUinfo.BlogPVNum.ToString();

            //关键字
            string keyword = binfo.KeyWord;
            hidKeyWord.Value = keyword;
            string keywordcontent = "";
            if (keyword == "")
            {
                keywordcontent = "暂无关键字";
            }
            else
            {
                keyword=keyword.Replace(",", " ");
                string[] strkeyword = keyword.Split(' ');
                for (int i = 0; i < strkeyword.Length; i++)
                {
                    keywordcontent += "<a href=\"../SearchResult.aspx?keyword=" + Server.UrlEncode(strkeyword[i]) + "\" target=\"_blank\">" + strkeyword[i] + "</a>";
                    keywordcontent += " ";
                }
            }
            litKeyWord.Text = keywordcontent;

            int moneytype = binfo.MoneyType;//收费类型 0免费1收费
            string costmoney = binfo.CostMoney.ToString();
            if (moneytype == 0)
            {
                lbContinue.Visible = false;
                lblShowPrompt.Visible = false;
                if (binfo.BlogBigType.ToLower() == "mp3")
                {
                    string content = "<script src=\"../JS/player.js\" type=\"text/javascript\"></script>";
                    content += "<script language=\"javascript\" type=\"text/javascript\">";
                    content += "Player(\""+binfo.FilePath+"\");";
                    content += "</script>";
                    content += "<a href=\"" + binfo.FilePath + "\" target=\"_blank\" style=\"color:green; font-size:12px\">下载mp3</a>";
                    litMusic.Text = content;
                }
                
                lblContent.Text = binfo.BlogContent.ToString();
            }
            else if (moneytype == 1)
            {
                //判断该用户是否已经查看过 如果已经查看过则不再次收费
                if (uinfo != null)
                {
                    //文章为当前用户的
                    if (binfo.UsersID == uinfo.ID)
                    {
                        lbContinue.Visible = false;
                        lblShowPrompt.Visible = false;
                        if (binfo.BlogBigType.ToLower() == "mp3")
                        {
                            string content = "<script src=\"../JS/player.js\" type=\"text/javascript\"></script>";
                            content += "<script language=\"javascript\" type=\"text/javascript\">";
                            content += "Player(\"" + binfo.FilePath + "\");";
                            content += "</script>";
                            content += "<a href=\"" + binfo.FilePath + "\" target=\"_blank\" style=\"color:green; font-size:12px\">下载mp3</a>";
                            litMusic.Text = content;
                        }
                        lblContent.Text = binfo.BlogContent.ToString();
                    }
                    else
                    {
                        PVHelper phelper = new PVHelper();
                        Criteria c = new Criteria();
                        c.Add(CriteriaType.Equals, "ObjectID", blogid);
                        c.Add(CriteriaType.Equals, "ObjectType", 1);
                        c.Add(CriteriaType.Equals, "UsersID", uinfo.ID);
                        int count = phelper.QueryPVCountBySearch(c);
                        if (count > 0)//查看过
                        {
                            lbContinue.Visible = false;
                            lblShowPrompt.Visible = false;
                            if (binfo.BlogBigType.ToLower() == "mp3")
                            {
                                string content = "<script src=\"../JS/player.js\" type=\"text/javascript\"></script>";
                                content += "<script language=\"javascript\" type=\"text/javascript\">";
                                content += "Player(\"" + binfo.FilePath + "\");";
                                content += "</script>";
                                content += "<a href=\"" + binfo.FilePath + "\" target=\"_blank\" style=\"color:green; font-size:12px\">下载mp3</a>";
                                litMusic.Text = content;
                            }
                            lblContent.Text = binfo.BlogContent.ToString();
                        }
                        else
                        {
                            //当前用户余额
                            float currentmoney = uhelper.GetCurrentMoney(uinfo.ID);//可用余额
                            if (currentmoney >= binfo.CostMoney)
                            {
                                lblShowPrompt.Text = "查看该创作需要花费" + costmoney + "个友赢豆";
                                lbContinue.Visible = true;
                            }
                            else
                            {
                                lblShowPrompt.Text = "查看该创作需要花费" + costmoney + "个友赢豆，您的友赢豆个数不足，请先   <a href=\"../UserCash/CashAdd.aspx\" target=\"_blank\"><font color=\"Red\">充值</font></a>";
                                lbContinue.Visible = false;
                            }
                        }
                    }
                }
                else
                {
                    lblShowPrompt.Text = "请先   <a href=\"../Login.aspx?returnurl=b/" + blogid + ".aspx\"><font color=\"Red\">登录</font></a>，才能查看该创作";
                    lbContinue.Visible = false;
                }

            }

            Users currentuinfo = UserBase.GetCurrentUser();
            if (currentuinfo != null)
            {
                hidCurrentUserID.Value = currentuinfo.ID;
            }
        }
        #endregion

        #region 提交评论
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string replycontent = txtReply.Text.Trim();
            if (string.IsNullOrEmpty(replycontent))
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "alert('评论内容不能为空！')", true);
                //Tools.Alert("评论内容不能为空！");
                return;
            }

            if (uinfo == null)
            {
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "alert('请先登录！')", true);
                //Tools.Alert("请先登录！");
                return;
            }

            Reply rinfo = new Reply();
            ReplyHelper rhelper = new ReplyHelper();
            rinfo.ObjectID = blogid;
            rinfo.ObjectType = 1;
            rinfo.UsersID = uinfo.ID;
            rinfo.NickName = uinfo.NickName;
            rinfo.ReplyContent = replycontent;
            rinfo.Flag = 0;

            rhelper.AddReply(rinfo);

            //博客表中的replycount增加1
            Blog binfo = new Blog();
            BlogHelper bhelper = new BlogHelper();
            binfo = bhelper.GetBlog(blogid);

            int replycount = binfo.ReplyCount;
            replycount = replycount + 1;
            binfo.ReplyCount = replycount;
            bhelper.EditBlog(binfo);

            //如果是回复内容不给博主发消息
            if (replycontent.Contains(":"))
            {
                string touserid = hidToUserID.Value;
                if (!string.IsNullOrEmpty(touserid))
                {
                    //给回复的用户发送通知消息
                    Message minfo = new Message();
                    MessageHelper mhelper = new MessageHelper();
                    minfo.FromUsersID = uinfo.ID;
                    minfo.FromNickName = uinfo.NickName;
                    minfo.ToUsersID = touserid;
                    minfo.Status = 0;
                    minfo.MessageContent = "<a href=\"../u/" + uinfo.ID + ".aspx\" target=\"_blank\">" + minfo.FromNickName + "</a>" + "在创作：<a href=\"../b/" + blogid + ".aspx\" target=\"_blank\">" + binfo.Title + "</a>中回复了你，<a href=\"../b/" + blogid + ".aspx#commenta\" target=\"_blank\">点击查看</a>";
                    minfo.Flag = 0;
                    mhelper.AddMessage(minfo);
                }

                txtReply.Text = "";
                LoadReply();
            }
            else
            {
                //给当前博客的用户发送通知消息
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                minfo.FromUsersID = uinfo.ID;
                minfo.FromNickName = uinfo.NickName;
                minfo.ToUsersID = binfo.UsersID;
                minfo.Status = 0;
                minfo.MessageContent = "<a href=\"../u/" + uinfo.ID + ".aspx\" target=\"_blank\">" + minfo.FromNickName + "</a>" + "评论了创作：<a href=\"../b/" + blogid + ".aspx\" target=\"_blank\">" + binfo.Title + "</a>，内容为：<a href=\"../b/" + blogid + ".aspx#commenta\" target=\"_blank\">" + replycontent + "</a>";
                minfo.Flag = 0;
                mhelper.AddMessage(minfo);

                txtReply.Text = "";
                LoadReply();
            }
        }
        #endregion

        #region 加载评论
        private void LoadReply()
        {
            //List<Member> list = new List<Member();
            ReplyHelper rhelper = new ReplyHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "ObjectID", blogid);
            c.Add(CriteriaType.Equals, "ObjectType", 1);
            c.Add(CriteriaType.Equals, "Flag", 0);

            //ReplyUPager.PrefixText = GetPreFixText(ReplyUPager);

            string[] fields = new string[] { "ID", "ObjectID", "ObjectType", "UsersID", "NickName", "ReplyContent", "CreateDate", "Flag" };

            DataTable dt = new DataTable();
            dt = rhelper.QueryReplyBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 50, fields);

            dt.Columns.Add(new DataColumn("headphoto"));

            if (dt != null && dt.Rows.Count > 0)
            {
                UsersHelper uhelper = new UsersHelper();
                foreach (DataRow dr in dt.Rows)
                {
                    string userid = dr["UsersID"] + "";
                    Users uinfo = new Users();
                    uinfo = uhelper.GetUsers(userid);
                    dr["headphoto"] = uinfo.SmallHeadImage;
                }
            }

            rptReply.DataSource = dt;
            rptReply.DataBind();

            litUserReplyCount.Text = dt.Rows.Count.ToString();
        }
        #endregion

        #region 加载作者火爆文章
        private void BindUserBlog()
        {
            string bloguserid = hidBlogUserID.Value;
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", bloguserid);
            c.Add(CriteriaType.NotEquals, "ID", blogid);
            c.Add(CriteriaType.Equals, "Flag", 0);

            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
            DataTable dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 10, fields);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtUserBlog.Columns.Add(dcTitle);
            dtUserBlog.Columns.Add(new DataColumn("strHasPic"));
            dtUserBlog.Columns.Add(new DataColumn("strVideoPic"));

            if (dtUserBlog != null && dtUserBlog.Rows.Count > 0)
            {
                foreach (DataRow dr in dtUserBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 32);
                    dr["strTitle"] = strtitle;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }
            }

            rptUserBlog.DataSource = dtUserBlog;
            rptUserBlog.DataBind();
        }
        #endregion

        #region 免费文章计算页面PV和用户分成
        private void BindPV()
        {
            int moneytype = binfo.MoneyType;

            PV pinfo = new PV();
            PVHelper phelper = new PVHelper();
            string ipaddress = IP.GetIPAddress();
            //判断今天是否已经查看过该页面
            //Criteria c = new Criteria();
            int count = 0;
            if (uinfo == null)
            {
                count = phelper.GetPVCountToday(blogid, "1", ipaddress, "");
            }
            else
            {
                count = phelper.GetPVCountToday(blogid, "1", ipaddress, uinfo.ID);
            }
            //c.Add(CriteriaType.Equals, "ObjectID", blogid);
            //c.Add(CriteriaType.Equals, "ObjectType", 1);

            //最多计算1次PV
            //int count = phelper.QueryPVCountBySearch(c);
            if (count == 0)
            {
                //增加pv表中的数据
                pinfo.IPAddress = ipaddress;
                pinfo.ObjectID = blogid;
                pinfo.ObjectType = 1;
                if (uinfo != null)
                {
                    pinfo.UsersID = uinfo.ID;
                }
                phelper.AddPV(pinfo);

                //增加博客表中的pvcount
                BlogHelper bhelper = new BlogHelper();
                int pvcount = binfo.PVCount;
                pvcount = pvcount + 1;
                binfo.PVCount = pvcount;
                bhelper.EditBlog(binfo);

                //增加该博主表中的blogpvnum
                string bloguserid = binfo.UsersID;
                UsersHelper uhelper = new UsersHelper();
                Users bloguserinfo = uhelper.GetUsers(bloguserid);
                int blogpvnum = bloguserinfo.BlogPVNum;
                blogpvnum = blogpvnum + 1;
                bloguserinfo.BlogPVNum = blogpvnum;
                uhelper.EditUsers(bloguserinfo);

                //增加该博客所属小组的blogpvnum
                string companyid = binfo.CompanyID;
                if (!string.IsNullOrEmpty(companyid) && companyid != "0")
                {
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(companyid);
                    if (cinfo != null)
                    {
                        int getblogpvnum = cinfo.BlogPVNum;
                        getblogpvnum = getblogpvnum + 1;
                        cinfo.BlogPVNum = getblogpvnum;
                        chelper.EditCompanys(cinfo);
                    }
                }

                if (blogpvnum % 100 == 0)
                {
                    //判断是否升级
                    RoleHelper rhelper = new RoleHelper();
                    rhelper.RoleUpgrade(bloguserid);
                }


                if (moneytype == 0)//免费的给作者分红
                {
                    //计算该博客作者的分红
                    //每1000pv计算一次 该值通过webconfig配置
                    int pvcountconfig = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["pvcountconfig"] + "");
                    if (pvcount % pvcountconfig == 0)
                    {
                        //查找该博客发表时用户的权限
                        string bloguserroleid = binfo.RoleID;
                        //string bloguserroleid = hidBlogUserRoleID.Value;
                        Role rinfo = new Role();
                        RoleHelper rhelper = new RoleHelper();
                        rinfo = rhelper.GetRole(bloguserroleid);
                        string pvratio = rinfo.PVRatio;
                        if (!string.IsNullOrEmpty(pvratio))
                        {
                            float fadvertradio = (float)Convert.ToDouble(pvratio);
                            if (fadvertradio > 0)
                            {
                                float getmoney = fadvertradio * pvcount;
                                //将该用户得到的金额存入到现金表中
                                CashFlow cfinfo = new CashFlow();
                                CashFlowHelper cfhelper = new CashFlowHelper();
                                cfinfo.UsersID = hidBlogUserID.Value;
                                cfinfo.Money = getmoney;
                                cfinfo.Description = binfo.Title + "页面分成";
                                cfinfo.Type = 2;//类型为页面分成
                                cfhelper.AddCashFlow(cfinfo);
                            }
                        }
                    }
                }
            }
        }
        #endregion

        #region 加载该用户推荐的
        private void BindRecommand()
        {
            RecommandHelper rhelper = new RecommandHelper();
            string bloguserid = hidBlogUserID.Value;
            DataTable dtpvBlog = rhelper.GetUserRecommand(bloguserid, "10", "1");

            if (dtpvBlog != null && dtpvBlog.Rows.Count > 0)
            {
                DataColumn dcTitle = new DataColumn("strTitle");
                dtpvBlog.Columns.Add(dcTitle);
                dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
                dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));
                foreach (DataRow dr in dtpvBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 32);
                    dr["strTitle"] = strtitle;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }

                rptRecommandOrder.DataSource = dtpvBlog;
                rptRecommandOrder.DataBind();
            }
            else
            {
                litNickName2.Text = "浏览榜";
                BlogHelper bhelper = new BlogHelper();
                Criteria c = new Criteria();
                c.Add(CriteriaType.Equals, "Flag", 0);

                string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
                DataTable dtpvBlog1 = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("PVCount", OrderMode.Desc) }, 0, 10, fields);

                DataColumn dcTitle1 = new DataColumn("strTitle");
                dtpvBlog.Columns.Add(dcTitle1);
                dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
                dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));

                if (dtpvBlog1 != null && dtpvBlog1.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtpvBlog1.Rows)
                    {
                        string title = dr["title"] + "";
                        string strtitle = "";
                        strtitle = Tools.GetFirstString(title, 26);
                        dr["strTitle"] = strtitle;

                        //判断是否存在图片
                        string haspic = dr["haspic"] + "";
                        if (haspic == "1")
                        {
                            dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                        }
                        else
                        {
                            dr["strHasPic"] = "";
                        }

                        string blogbigtype = dr["BlogBigType"] + "";
                        if (blogbigtype == "视频")
                        {
                            dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                        }
                        else if (blogbigtype.ToLower() == "mp3")
                        {
                            dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                        }
                        else
                        {
                            dr["strVideoPic"] = "";
                        }
                    }
                }
                rptRecommandOrder.DataSource = dtpvBlog1;
                rptRecommandOrder.DataBind();
            }

            
        }
        #endregion

        #region 加载SEO信息
        private void BindSeo()
        {
            string author = System.Configuration.ConfigurationManager.AppSettings["author"] + "";
            string copyright = System.Configuration.ConfigurationManager.AppSettings["copyright"] + "";

            string title = litTitle.Text + "-" + author;
            string nickname = litNickName.Text;
            string keywordcontent = hidKeyWord.Value;//该篇文章关键字
            string systemkeyword = System.Configuration.ConfigurationManager.AppSettings["keyword"] + "";



            string keyword = nickname + " " + keywordcontent + " " + systemkeyword + " " + author;
            string content = lblContent.Text;
            //截取内容
            string blogcontent = Server.HtmlDecode(content);
            //将内容中的html代码去掉
            blogcontent = Tools.NoHTML(blogcontent);
            string strblogcontent = Tools.GetFirstString(blogcontent, 200);
            string description = strblogcontent;
            if (description == "")
            {
                description = title;
            }

            strseo += "<title>" + title + "</title>\r\n";
            strseo += "<meta name=\"keywords\" content=\"" + keyword + "\" />\r\n";
            strseo += "<meta name=\"description\" content=\"" + description + "\" />\r\n";
            strseo += "<meta name=\"author\" content=\"" + author + "\" />\r\n";
            strseo += "<meta name=\"Copyright\" content=\"" + copyright + "\" />\r\n";
        }
        #endregion

        #region 继续查看
        protected void lbContinue_Click(object sender, EventArgs e)
        {
            //判断用户是否登录
            if (uinfo == null)
            {
                Tools.AlertAndRedirect("请先登录", "../Login.aspx");
                return;
            }
            else
            {
                //判断当前用户的可用余额
                UsersHelper uhelper = new UsersHelper();
                float currentmoney = uhelper.GetCurrentMoney(uinfo.ID);//可用余额
                float costmoney = (float)binfo.CostMoney;
                if (currentmoney >= costmoney)//当前余额大于该文章需要的金额
                {
                    //给当前用户减去文章的金额
                    CashFlowHelper cfhelper = new CashFlowHelper();
                    CashFlow cfinfojian = new CashFlow();
                    cfinfojian.UsersID = uinfo.ID;
                    cfinfojian.Money = costmoney * -1;
                    cfinfojian.Description = "阅读：<a href=\"../b/" + blogid + ".aspx\" target=\"_blank\">" + binfo.Title + "</a>";
                    cfinfojian.Type = 4;
                    cfhelper.AddCashFlow(cfinfojian);
                    //给文章的用户添加文章的金额
                    CashFlow cfinfojia = new CashFlow();
                    cfinfojia.UsersID = hidBlogUserID.Value;
                    cfinfojia.Money = costmoney;
                    cfinfojia.Description = "创作<a href=\"../b/" + blogid + ".aspx\" target=\"_blank\">" + binfo.Title + "</a>被阅读";
                    cfinfojia.Type = 4;
                    cfhelper.AddCashFlow(cfinfojia);

                    //将该用户插入到PV表中 下次查看不进行收费
                    //增加pv表中的数据
                    string ipaddress = IP.GetIPAddress();
                    PV pinfo = new PV();
                    PVHelper phelper = new PVHelper();
                    pinfo.IPAddress = ipaddress;
                    pinfo.ObjectID = blogid;
                    pinfo.ObjectType = 1;
                    pinfo.UsersID = uinfo.ID;
                    phelper.AddPV(pinfo);

                    //增加博客表中的pvcount
                    BlogHelper bhelper = new BlogHelper();
                    int pvcount = binfo.PVCount;
                    pvcount = pvcount + 1;
                    binfo.PVCount = pvcount;
                    bhelper.EditBlog(binfo);

                    //增加该博主表中的blogpvnum
                    string bloguserid = binfo.UsersID;
                    //UsersHelper uhelper = new UsersHelper();
                    Users bloguserinfo = uhelper.GetUsers(bloguserid);
                    int blogpvnum = bloguserinfo.BlogPVNum;
                    blogpvnum = blogpvnum + 1;
                    bloguserinfo.BlogPVNum = blogpvnum;
                    uhelper.EditUsers(bloguserinfo);

                    //增加该博客所属小组的blogpvnum
                    string companyid = binfo.CompanyID;
                    if (!string.IsNullOrEmpty(companyid) && companyid != "0")
                    {
                        Companys cinfo = new Companys();
                        CompanysHelper chelper = new CompanysHelper();
                        cinfo = chelper.GetCompanys(companyid);
                        if (cinfo != null)
                        {
                            int getblogpvnum = cinfo.BlogPVNum;
                            getblogpvnum = getblogpvnum + 1;
                            cinfo.BlogPVNum = getblogpvnum;
                            chelper.EditCompanys(cinfo);
                        }
                    }

                    if (blogpvnum % 100 == 0)
                    {
                        //判断是否升级
                        RoleHelper rhelper = new RoleHelper();
                        rhelper.RoleUpgrade(bloguserid);
                    }

                    //显示文章内容
                    Response.Redirect("../b/" + blogid + ".aspx");
                    return;
                }
                else//当前余额小于于该文章需要的金额
                {
                    //需进行充值
                    Tools.Alert("请进行充值");
                    return;
                }
            }
        }
        #endregion

        #region 加载前博客
        private void BindFrontBlog()
        {
            string userid = hidBlogUserID.Value;
            DateTime createdate = binfo.CreateDate;
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "UsersID", userid);
            c.Add(CriteriaType.LessThan, "CreateDate", createdate);

            string[] fields = new string[] { "ID", "BlogBigType", "Title", "HasPic", "Flag", "CreateDate" };

            DataTable dt = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 1, fields);

            if (dt != null && dt.Rows.Count > 0)
            {
                string frontblogid = dt.Rows[0]["ID"] + "";
                string title = dt.Rows[0]["Title"] + "";
                string blogbigtype = dt.Rows[0]["BlogBigType"] + "";
                string haspic = dt.Rows[0]["HasPic"] + "";
                title = Tools.GetFirstString(title,60);
                if (blogbigtype == "视频")
                {
                    lblFrontBlog.Text = "作者前一篇：<img src=\"../images/smallvideo.jpg\" alt=\"视频\"><a href=\"../b/" + frontblogid + ".aspx\">" + title + "</a>";
                }
                else if (blogbigtype.ToLower() == "mp3")
                {
                    lblFrontBlog.Text = "作者前一篇：<img src=\"../images/mp3.png\" alt=\"mp3\"><a href=\"../b/" + frontblogid + ".aspx\">" + title + "</a>";
                }
                else if (haspic == "1")
                {
                    lblFrontBlog.Text = "作者前一篇：<a href=\"../b/" + frontblogid + ".aspx\">" + title + "</a><img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\">";
                }
                else
                {
                    lblFrontBlog.Text = "作者前一篇：<a href=\"../b/" + frontblogid + ".aspx\">" + title + "</a>";
                }
            }
        }
        #endregion

        #region 加载后博客
        private void BindBehindBlog()
        {
            string userid = hidBlogUserID.Value;
            DateTime createdate = binfo.CreateDate;
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "UsersID", userid);
            c.Add(CriteriaType.MoreThan, "CreateDate", createdate);
            string[] fields = new string[] { "ID", "BlogBigType", "Title", "HasPic", "Flag", "CreateDate" };

            DataTable dt = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Asc) }, 0, 1, fields);

            if (dt != null && dt.Rows.Count > 0)
            {
                string behindblogid = dt.Rows[0]["ID"] + "";
                string title = dt.Rows[0]["Title"] + "";
                string blogbigtype = dt.Rows[0]["BlogBigType"] + "";
                string haspic = dt.Rows[0]["HasPic"] + "";
                title = Tools.GetFirstString(title, 60);
                if (blogbigtype == "视频")
                {
                    lblBehindBlog.Text = "作者后一篇：<img src=\"../images/smallvideo.jpg\" alt=\"视频\"><a href=\"../b/" + behindblogid + ".aspx\">" + title + "</a>";
                }
                else if (blogbigtype.ToLower() == "mp3")
                {
                    lblBehindBlog.Text = "作者后一篇：<img src=\"../images/mp3.png\" alt=\"mp3\"><a href=\"../b/" + behindblogid + ".aspx\">" + title + "</a>";
                }
                else if (haspic == "1")
                {
                    lblBehindBlog.Text = "作者后一篇：<a href=\"../b/" + behindblogid + ".aspx\">" + title + "</a><img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\">";
                }
                else
                {
                    lblBehindBlog.Text = "作者后一篇：<a href=\"../b/" + behindblogid + ".aspx\">" + title + "</a>";
                }
            }
        }
        #endregion

        #region 加载同分类其它创作
        private void BindOtherBlog()
        {
            string blogtypeid = hidBlogTypeID.Value;
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            Criteria c1 = new Criteria();
            c.Add(CriteriaType.Equals, "BlogTypeID", blogtypeid);
            c.Add(CriteriaType.NotEquals, "ID", blogid);
            c.Add(CriteriaType.Equals, "Flag", 0);
            c1.Add(CriteriaType.Equals, "BlogTypeID", blogtypeid);
            c1.Add(CriteriaType.NotEquals, "ID", blogid);
            c1.Add(CriteriaType.Equals, "Flag", 0);
            c1.Add(CriteriaType.MoreThanEquals, "CreateDate", binfo.CreateDate);
            //判断该分类的大于该创作时间的个数
            int count = bhelper.QueryBlogCountBySearch(c1);
            DataTable dtUserBlog = null;
            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
            if (count >= 12)
            {
                c.Add(CriteriaType.MoreThanEquals, "CreateDate", binfo.CreateDate);
                dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Asc) }, 0, 12, fields);
            }
            else
            {
                dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 12, fields);
            }

            
            //DataTable dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Asc) }, 0, 12, fields);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtUserBlog.Columns.Add(dcTitle);
            dtUserBlog.Columns.Add(new DataColumn("strHasPic"));
            dtUserBlog.Columns.Add(new DataColumn("strVideoPic"));

            if (dtUserBlog != null && dtUserBlog.Rows.Count > 0)
            {
                foreach (DataRow dr in dtUserBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 90);
                    dr["strTitle"] = strtitle;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }
                divOther.Visible = true;
                rptOtherBlog.DataSource = dtUserBlog;
                rptOtherBlog.DataBind();
            }
            else
            {
                divOther.Visible = false;
            }

            
        }
        #endregion

        #region 加载小组信息
        private void BindCompany()
        {
            string companyid = binfo.CompanyID;
            blogcompanyid = companyid;
            if (string.IsNullOrEmpty(companyid)||companyid=="0")
            {
                divCompany.Visible = false;
                divPerson.Visible = true;
            }
            else
            {
                divCompany.Visible = true;
                divPerson.Visible = false;
                //当前公司数据
                Companys cinfo = new Companys();
                CompanysHelper chelper = new CompanysHelper();
                cinfo = chelper.GetCompanys(companyid);
                string realmname = cinfo.RealmName;
                litCompanyName.Text = cinfo.CompanyName;
                hidCompanyID.Value = companyid;
                hidObjectID.Value = companyid;
                companylink = "../c/"+companyid+".aspx";
                litCompanyBrief.Text = cinfo.Brief;
                litOwnerNickName.Text = cinfo.OwnerNickName;
                litUserNum.Text = cinfo.UserNum.ToString();
                litCareNum.Text = cinfo.CareNum.ToString();
                litCompanyBlogPVNum.Text = cinfo.BlogPVNum.ToString();
                ownerlink = "../u/" + cinfo.OwnerUserID + ".aspx" ;
                string smallheadimage = cinfo.SmallHeadImage;
                if (string.IsNullOrEmpty(smallheadimage))
                {
                    imgCompanyHead.ImageUrl = "../images/xiaozuphoto.jpg";
                }
                else
                {
                    imgCompanyHead.ImageUrl = cinfo.SmallHeadImage;
                }
                imgCompanyHead.ToolTip = cinfo.CompanyName;
                //imgCompanyHead.ImageUrl = cinfo.SmallHeadImage;
                companyowneruserid = cinfo.OwnerUserID;

                string notice = cinfo.Notice;
                if (string.IsNullOrEmpty(notice))
                {
                    notice = "暂无";
                }
                litCompanyNotice.Text = notice;
            }
        }
        #endregion

        #region 加载同小组博客
        private void BindCompanyBlog()
        {
            string companyid = binfo.CompanyID;
            if (string.IsNullOrEmpty(companyid) || companyid == "0")
            {
                divCompanyBlog.Visible = false;
            }
            else
            {
                
                divCompanyBlog.Visible = true;
                //同小组其它博客
                BlogHelper bhelper = new BlogHelper();
                Criteria c = new Criteria();
                Criteria c1 = new Criteria();
                c.Add(CriteriaType.Equals, "CompanyID", companyid);
                c.Add(CriteriaType.NotEquals, "ID", blogid);
                c.Add(CriteriaType.Equals, "Flag", 0);

                c1.Add(CriteriaType.Equals, "CompanyID", companyid);
                c1.Add(CriteriaType.NotEquals, "ID", blogid);
                c1.Add(CriteriaType.MoreThanEquals, "CreateDate", binfo.CreateDate);
                c1.Add(CriteriaType.Equals, "Flag", 0);
                //判断该小组中时间大于该创作日期的个数
                int count = bhelper.QueryBlogCountBySearch(c1);
                DataTable dtUserBlog = null;
                string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
                if (count >= 12)
                {
                    c.Add(CriteriaType.MoreThanEquals, "CreateDate", binfo.CreateDate);
                    dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Asc) }, 0, 12, fields);
                }
                else
                {
                    dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 12, fields);
                }

                //DataTable dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Asc) }, 0, 12, null);

                DataColumn dcTitle = new DataColumn("strTitle");
                dtUserBlog.Columns.Add(dcTitle);
                dtUserBlog.Columns.Add(new DataColumn("strHasPic"));
                dtUserBlog.Columns.Add(new DataColumn("strVideoPic"));

                if (dtUserBlog != null && dtUserBlog.Rows.Count > 0)
                {
                    foreach (DataRow dr in dtUserBlog.Rows)
                    {
                        string title = dr["title"] + "";
                        string strtitle = "";
                        strtitle = Tools.GetFirstString(title, 90);
                        dr["strTitle"] = strtitle;

                        //判断是否存在图片
                        string haspic = dr["haspic"] + "";
                        if (haspic == "1")
                        {
                            dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                        }
                        else
                        {
                            dr["strHasPic"] = "";
                        }

                        string blogbigtype = dr["BlogBigType"] + "";
                        if (blogbigtype == "视频")
                        {
                            dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                        }
                        else if (blogbigtype.ToLower() == "mp3")
                        {
                            dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                        }
                        else
                        {
                            dr["strVideoPic"] = "";
                        }
                    }
                    divOther.Visible = false;
                    divCompanyBlog.Visible = true;
                    rptCompanyBlog.DataSource = dtUserBlog;
                    rptCompanyBlog.DataBind();
                }
                else
                {
                    divCompanyBlog.Visible = false;
                }
            }
        }
        #endregion

        #region 加载广告
        private void BindAd()
        {
            AdPositionHelper aphelper = new AdPositionHelper();
            string result1 = aphelper.BindAd(youbloguserid, "11", 1);
            litAd1.Text = result1;
            string result2 = aphelper.BindAd(youbloguserid, "12", 1);
            litAd2.Text = result2;
            string result3 = aphelper.BindAd(youbloguserid, "13", 1);
            litAd3.Text = result3;
            string result4 = aphelper.BindAd(youbloguserid, "14", 1);
            litAd4.Text = result4;
            string result5 = aphelper.BindAd(youbloguserid, "15", 1);
            litAd5.Text = result5;
            string result6 = aphelper.BindAd(youbloguserid, "16", 1);
            litAd6.Text = result6;
        }
        #endregion

        #region 加载热门小组列表
        private void BindCompanyList()
        {
            CompanysHelper chelper = new CompanysHelper();
            Criteria c = new Criteria();

            //加载有效的
            c.Add(CriteriaType.Equals, "Flag", 0);

            string[] fields = new string[] { "ID", "CompanyName", "RealmName", "Brief", "BlogPVNum", "SmallHeadImage", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = chelper.QueryCompanysBySearchToTable(c, new Order[] { new Order("BlogPVNum", OrderMode.Desc) }, 0, 6, fields);
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string smallheadimage = dr["SmallHeadImage"] + "";
                    if (string.IsNullOrEmpty(smallheadimage))
                    {
                        dr["SmallHeadImage"] = "../images/xiaozuphoto.jpg";
                    }
                }
            }

            dlCompanyList.DataSource = dt;
            dlCompanyList.DataBind();
        }
        #endregion
    }
}